package com.open.exam.mapper;

import com.open.exam.databasemodel.ExamPlanInfo;
import com.open.exam.dto.ExamPlanDetailViewModel;
import com.open.exam.dto.ExamineePlanInfo;
import com.open.exam.sqlprovider.ExamPlanInfoProvider;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

/**
 * @author 奶茶
 * Created on 2017/11/16.
 */
@Mapper
public interface ExamPlanInfoMapper {

    @Select("SELECT * FROM ExamPlanInfo WHERE AppID = #{appID} AND ExamPlanInfoID = #{examPlanInfoID} LIMIT 1")
    ExamPlanInfo getExamPlanInfo(@Param(value = "appID") String appID, @Param(value = "examPlanInfoID") String examPlanInfoID);

    @Select("SELECT * FROM ExamPlanInfo WHERE AppID = #{appID} AND ExamPlanInfoID = #{examPlanInfoID} AND SysState != #{sysState} LIMIT 1")
    ExamPlanInfo getExamPlanInfoBySysState(@Param(value = "appID") String appID, @Param(value = "examPlanInfoID") String examPlanInfoID, @Param(value = "sysState") Integer sysState);

    @Select("SELECT ExamPlanInfo.DeploymentMode FROM ExamPlanInfo WHERE AppID = #{appID} AND ExamPlanInfoID = #{examPlanInfoID} LIMIT 1")
    Integer getDeploymentMode(@Param(value = "appID") String appID, @Param(value = "examPlanInfoID") String examPlanInfoID);

    @SelectProvider(type = ExamPlanInfoProvider.class,method = "findExamPlanDetailViewModelListBySomeWhere")
    List<ExamPlanDetailViewModel> findExamPlanDetailViewModelListBySomeWhere(@Param(value = "planId") String planId, @Param(value = "roomId") String roomId, @Param(value = "sceneId") String sceneId, @Param(value = "subjectId") String subjectId, @Param(value = "appId") String appId, @Param(value = "siteId") String siteId);

    @Select("SELECT * FROM ExamPlanInfo WHERE ExamPlanInfoID = #{examPlanInfoID} AND SysState = #{sysState}")
    List<ExamPlanInfo> findExamPlanListByIdAndState(@Param(value = "examPlanInfoID") String examPlanInfoID, @Param(value = "sysState") Integer sysState);

    @Select("SELECT * FROM ExamPlanInfo WHERE ExamPlanInfoID = #{examPlanInfoID}")
    List<ExamPlanInfo> findExamPlanInfoList(@Param(value = "examPlanInfoID") String examPlanInfoID);

    @Insert("INSERT INTO ExamPlanInfo (\n" +
            "\tAppID,ExamSiteID,ExamPlanInfoID,ExamPlanCode,ExamPlanName,ExamPlanType,BeginTime,EndTime,ExamMode,LoginMode,MinExamTime,RequiredSignIn,SeatByNumberRequired,SysState,ArrangeMode,CapturePhotoMaxNumber,DeploymentMode\n" +
            ") SELECT #{appID},#{examSiteID},#{examPlanInfoID},#{examPlanCode},#{examPlanName},#{examPlanType},#{beginTime},#{endTime},#{examMode},#{loginMode},#{minExamTime},#{requiredSignIn},#{seatByNumberRequired},#{sysState},#{arrangeMode},#{capturePhotoMaxNumber},#{deploymentMode}\n" +
            "FROM\n" +
            "\tDUAL\n" +
            "WHERE\n" +
            "\tNOT EXISTS (\n" +
            "\t\tSELECT\n" +
            "\t\t\t1\n" +
            "\t\tFROM\n" +
            "\t\t\tExamPlanInfo\n" +
            "\t\tWHERE\n" +
            "\t\t\tExamPlanInfoID = #{examPlanInfoID}\n" +
            "\t\tLIMIT 1\n" +
            ")")
    Integer insertExamPalnInfo(ExamPlanInfo examPlanInfo);

    @Select("SELECT * FROM ExamPlanInfo WHERE ExamPlanInfoID = #{planId} LIMIT 1")
    ExamPlanInfo findExamPlanInfoFirst(@Param("planId") String planId);

    @Delete("DELETE FROM ExamPlanInfo WHERE ExamPlanInfoID = #{planId}")
    void delExamPlanInfoByPlanId(@Param("planId") String planId);

    @Update("UPDATE ExamPlanInfo SET SysState = #{sysState} WHERE AppID = #{appID} AND ExamSiteID = #{examSiteID} AND ExamPlanInfoID = #{examPlanInfoID}")
    Integer updateExamPalnSyState(ExamPlanInfo examPlanInfo);

    @Select("SELECT * FROM ExamPlanInfo WHERE ExamPlanType = #{examPlanType} AND AppID = #{appID} AND ExamSiteID = #{examSiteID}")
    List<ExamPlanInfo> findExamPlanInfoLists(@Param(value = "examPlanType") Short examPlanType, @Param(value = "appID") String appID, @Param(value = "examSiteID") String examSiteID);

    @Select("SELECT * FROM ExamPlanInfo WHERE ExamPlanType = #{examPlanType} AND AppID = #{appId} AND ExamMode = #{examMode}")
    List<ExamPlanInfo> findExamPlanInfoByAppIdAndModeAndType(@Param(value = "appId") String appId, @Param(value = "examMode") Integer examMode, @Param(value = "examPlanType") Integer examPlanType);

    @SelectProvider(type = ExamPlanInfoProvider.class,method = "findExamineePlanInfoList")
    List<ExamineePlanInfo> findExamineePlanInfoList(@Param("examPlanInfoIds") List<String> examPlanInfoIds,
                                                    @Param("examineeIds") List<String> examineeIds,
                                                    @Param("submited") Integer submited, @Param("testPaperCreated") Integer testPaperCreated,
                                                    @Param("timeNow") String timeNow);

    @Select("SELECT * FROM ExamPlanInfo WHERE ExamPlanType = #{examPlanType} AND AppID = #{appId}")
    List<ExamPlanInfo> findExamPlanInfoByAppIdAndType(@Param(value = "appId") String appId, @Param(value = "examPlanType") Integer examPlanType);

    @Select("SELECT * FROM ExamPlanInfo WHERE AppID = #{appID}")
    List<ExamPlanInfo> findAllExamPlan(@Param(value = "appID") String appID);

    @Select("SELECT\n" +
            "\texamSubject.subjectPaperStatus,\n" +
            "\tCOUNT(\n" +
            "\t\texamSubject.SubjectPaperStatus\n" +
            "\t) count\n" +
            "FROM\n" +
            "\tExamPlanInfo examPlan\n" +
            "JOIN ExamSubject examSubject ON examPlan.ExamPlanInfoID = examSubject.ExamPlanInfoID\n" +
            "WHERE\n" +
            "\texamPlan.ExamPlanInfoID = #{examPlanID}\n" +
            "GROUP BY\n" +
            "\texamSubject.SubjectPaperStatus")
    List<Map<String,Object>> findExamPlanSubject(@Param(value = "examPlanID") String examPlanID);

    @Select("SELECT AppID,ExamSiteID,ExamPlanInfoID FROM examplaninfo WHERE BeginTime < NOW() AND SysState = 6")
    List<ExamPlanInfo> findAllStartExamPlanInfo();
}
